package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 商品信息更新监听器
 *
 * @author Moozy
 * @since 2025/5/6
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class ItemListener {

    private final ItemClient itemClient;

    private final RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.100.128:9200")
    ));

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.index.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = {"item.index"}
    ))
    public void listenItemIndex(Long itemId) throws IOException {
        log.info("新增 ES 商品信息: {}", itemId);
        // 获取数据库信息
        ItemDTO itemDTO = itemClient.queryItemById(itemId);
        if (itemDTO == null) {
            return;
        }
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);

        // ES request
        IndexRequest request = new IndexRequest("items")
                .id(String.valueOf(itemId))
                .source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.update.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = {"item.update"}
    ))
    public void listenItemUpdate(Long itemId) throws IOException {
        log.info("更新 ES 商品信息: {}", itemId);
        // 获取数据库信息
        ItemDTO itemDTO = itemClient.queryItemById(itemId);
        if (itemDTO == null) {
            return;
        }
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);

        // ES request
        UpdateRequest request = new UpdateRequest("items", itemDoc.getId())
                .doc(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        client.update(request, RequestOptions.DEFAULT);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.delete.queue", durable = "true"),
            exchange = @Exchange(name = "search.direct"),
            key = {"item.delete"}
    ))
    public void listenItemDelete(Long itemId) throws IOException {
        log.info("删除 ES 商品信息: {}", itemId);
        DeleteRequest request = new DeleteRequest("items", String.valueOf(itemId));
        client.delete(request, RequestOptions.DEFAULT);
    }

}
